package com.hhq.sso.system.controller;

import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hhq.sso.common.api.CommonResult;
import com.hhq.sso.common.page.Condition;
import com.hhq.sso.common.page.Query;
import com.hhq.sso.system.entity.DataScope;
import com.hhq.sso.system.service.IDataScopeService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;

/**
 * 数据权限控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/data-scope")
@Tag(name = "数据权限", description = "数据权限")
public class DataScopeController {

    private final IDataScopeService dataScopeService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @Operation(summary = "详情", description = "传入dataScope")
    public CommonResult<DataScope> detail(DataScope dataScope) {
        DataScope detail = dataScopeService.getOne(Condition.getQueryWrapper(dataScope));
        return CommonResult.data(detail);
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @Operation(summary = "分页", description = "传入dataScope")
    public CommonResult<IPage<DataScope>> list(DataScope dataScope, Query query) {
        IPage<DataScope> pages = dataScopeService.page(Condition.getPage(query), Condition.getQueryWrapper(dataScope));
        return CommonResult.data(pages);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @Operation(summary = "新增", description = "传入dataScope")
    public CommonResult<Boolean> save(@Valid @RequestBody DataScope dataScope) {
        return CommonResult.status(dataScopeService.save(dataScope));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @Operation(summary = "修改", description = "传入dataScope")
    public CommonResult<Boolean> update(@Valid @RequestBody DataScope dataScope) {
        return CommonResult.status(dataScopeService.updateById(dataScope));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Operation(summary = "新增或修改", description = "传入dataScope")
    public CommonResult<Boolean> submit(@Valid @RequestBody DataScope dataScope) {
        return CommonResult.status(dataScopeService.saveOrUpdate(dataScope));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @Operation(summary = "逻辑删除", description = "传入ids")
    public CommonResult<Boolean> remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
        return CommonResult.status(dataScopeService.deleteLogic(Func.toLongList(ids)));
    }

}
